package controllers

import (
	"strings"

	"newAi/new_web/models"

)

type GatewayGroupController struct {
	BaseController
}

func (self *GatewayGroupController) List() {
	self.Data["pageTitle"] = "网关组管理"
	self.Data["auth"] = false
	if self.userId == 1 {
		self.Data["auth"] = true
	}

	self.display()
}

func (self *GatewayGroupController) GroupAdd() {
	if self.isOrg ==true{
		self.SetUserGroup(self.userId)
	}else{
		self.SetAllGroup()
	}
	self.Data["pageTitle"] = "新增网关组"
	self.display()
}

func (self *GatewayGroupController) GroupEdit() {
	self.Data["pageTitle"] = "编辑网关组"

	id, _ := self.GetInt("id", 0)
	GatewayGroup, _ := models.GatewayGroupGetById(id)
	row := make(map[string]interface{})
	row["id"] = GatewayGroup.Id
	row["group_name"] = GatewayGroup.Group_name
	row["group_desc"] = GatewayGroup.Group_desc
	row["group_user_id"] = GatewayGroup.Group_user_id
	self.Data["group"] = row
	if self.isOrg ==true{
		self.SetUserGroup(self.userId)
	}else{
		self.SetAllGroup()
	}
	self.display()

}

func (self *GatewayGroupController) AjaxSave() {
	GatewayGroup_id, _ := self.GetInt("id")
	if GatewayGroup_id == 0 {
		GatewayGroup := new(models.GatewayGroup)
		GatewayGroup.Group_name = strings.TrimSpace(self.GetString("group_name"))
		GatewayGroup.Group_desc= self.GetString("group_desc")
		GatewayGroup.Group_user_id,_= self.GetInt("group_user_id")
		// 检查登录名是否已经存在
		_, err := models.GatewayGroupGetByName(GatewayGroup.Group_name)

		if err == nil {
			self.ajaxMsg("名称已经存在", MSG_ERR)
		}
		//新增
		if _, err := models.GatewayGroupAdd(GatewayGroup); err != nil {
			self.ajaxMsg(err.Error(), MSG_ERR)
		}
		self.ajaxMsg("", MSG_OK)
	}

	GatewayGroup, _ := models.GatewayGroupGetById(GatewayGroup_id)
	//修改

	GatewayGroup.Group_name = strings.TrimSpace(self.GetString("group_name"))
	GatewayGroup.Group_desc= self.GetString("group_desc")
	GatewayGroup.Group_user_id,_= self.GetInt("group_user_id")

	if err := GatewayGroup.Update(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *GatewayGroupController) AjaxDel() {

	GatewayGroup_id, _ := self.GetInt("id")
	GatewayGroup, _ := models.GatewayGroupGetById(GatewayGroup_id)

	if err := GatewayGroup.Delete(); err != nil {
		self.ajaxMsg(err.Error(), MSG_ERR)
	}
	self.ajaxMsg("", MSG_OK)
}

func (self *GatewayGroupController) GroupTable() {
	//列表
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 30
	}

	self.pageSize = limit
	//查询条件
	user_group, err := models.UserGroupIdName()
	self.CheckError(err)
	filters := make([]interface{}, 0)
	if self.isOrg == true {
		filters = append(filters, "group_user_id", self.userId)
	}
	result, count := models.GatewayGroupGetList(page, self.pageSize, filters...)
	list := make([]map[string]interface{}, len(result))
	for k, v := range result {
		row := make(map[string]interface{})
		row["id"] = v.Id
		row["group_name"] = v.Group_name
		for _, b := range user_group {
			if v.Group_user_id ==b.Id {
				row["group_user_id"] = b.LoginName
			}
		}
		row["group_desc"] = v.Group_desc
		list[k] = row
	}
	self.ajaxList("成功", MSG_OK, count, list)
}
